package com.microcity.autocomponent.autoview.viewex.pie;

import android.content.Context;
import android.graphics.Canvas;
import android.graphics.Paint;
import android.graphics.RectF;
import android.support.annotation.Nullable;
import android.util.AttributeSet;
import android.util.Log;
import android.view.View;

import java.util.ArrayList;
import java.util.List;

/**
 * User: yaodong(1126220529@qq.com)
 * Date: 2017-05-17
 * Time: 00:27
 * FIXME
 */


public class YDPieView extends View {

    // 颜色表(注意: 此处定义颜色使用的是ARGB，带Alpha通道的)
    private int[] mColors = {0xFFCCFF00, 0xFF6495ED, 0xFFE32636, 0xFF800000, 0xFF808000, 0xFFFF8C69, 0xFF808080,
            0xFFE6B800, 0xFF7CFC00};

    //饼图起始角度 0
    private float mStartAngle = 0;
    //饼图数据源
    private ArrayList<PieData> mDatas;
    //画笔
    private Paint mPaint = new Paint(Paint.ANTI_ALIAS_FLAG);
    //宽高
    private int mWidth, mHeight;

    public YDPieView(Context context) {
        this(context, null, 0);
    }

    public YDPieView(Context context, @Nullable AttributeSet attrs) {
        this(context, attrs, 0);
    }

    public YDPieView(Context context, @Nullable AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        initPaint();
    }

    private void initPaint() {
        mPaint.setStyle(Paint.Style.FILL);
    }

    @Override
    protected void onSizeChanged(int w, int h, int oldw, int oldh) {
        super.onSizeChanged(w, h, oldw, oldh);
        mWidth = w;
        mHeight = h;
    }

    @Override
    protected void onDraw(Canvas canvas) {
        super.onDraw(canvas);
        initPie(canvas);
    }

    private void initPie(Canvas canvas) {
        if (null == mDatas) {
            return;
        }
        float currentStartAngle = mStartAngle;//当前起始角度
        canvas.translate(mWidth / 2, mHeight / 2);//将画布坐标原点移动到中心位置  平移画布
        float r = (float) (Math.min(mWidth, mHeight) / 2 * 0.8);//饼图半径
        RectF rectF = new RectF(-r, -r, r, r);
        for (int i = 0; i < mDatas.size(); i++) {
            PieData pie = mDatas.get(i);
            mPaint.setColor(pie.getColor());
            canvas.drawArc(rectF, currentStartAngle, pie.getAngle(), true, mPaint);
            currentStartAngle += pie.getAngle();
        }
    }

    //设置起始角度
    public void setStartAngle(int startAngle) {
        this.mStartAngle = startAngle;
        invalidate();
    }

    //设置数据
    public void setData(ArrayList<PieData> mDatas) {
        this.mDatas = mDatas;
        initData(mDatas);

        invalidate();
    }

    //加载数据
    private void initData(List<PieData> mDatas) {
        if (null == mDatas || mDatas.size() == 0) {//数据有问题直接返回
            return;
        }

        float sumValue = 0;

        for (int i = 0; i < mDatas.size(); i++) {
            PieData pieData = mDatas.get(i);
            sumValue += pieData.getValue();//数值总和
            int j = i % mColors.length;//设置颜色
            pieData.setColor(mColors[j]);
        }

        float sumAngle = 0;

        for (int i = 0; i < mDatas.size(); i++) {
            PieData pieData = mDatas.get(i);
            float percentage = pieData.getValue() / sumValue;//百分比
            float angle = percentage * 360; //对应角度
            pieData.setPersentage(percentage);//记录百分比
            pieData.setAngle(angle);//记录角度
            sumAngle += angle;

            Log.i("angle", "" + pieData.getAngle());
        }


    }

}
